package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 设置昵称
 * Created by PoisonH on 2016/6/28.
 */
public class SettingMyNameActivity extends BaseActivity implements View.OnClickListener
{
    private EditText mSettingName;
    private String mStrName;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_settingmyname);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("设置昵称");
        mRightBtn.setVisibility(View.GONE);
        mTvRight.setVisibility(View.VISIBLE);
        mLeftBtn.setOnClickListener(this);
        mTvRight.setText("保存");
        mTvRight.setOnClickListener(this);
        mSettingName = (EditText) findViewById(R.id.settingname_input_name);
        if (mStrUserName != null)
        {
            mSettingName.setText(mStrUserName);
            Editable etext = mSettingName.getText();
            Selection.setSelection(etext, etext.length());
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.tv_right_txt:
                mStrName = mSettingName.getText().toString().trim();
                if (!TextUtils.isEmpty(mStrName))
                {
                    if (mStrName.equals(mStrUserName))
                    {
                        ToastUtils.showToast(SettingMyNameActivity.this, "昵称未发生改变");
                    } else
                    {
                        updateName();
                        SVProgressHUD.showWithStatus(this, "加载中...");
                    }
                } else
                {
                    ToastUtils.showToast(SettingMyNameActivity.this, "昵称不能为空");
                }
                break;
        }
    }

    /**
     * 更新用户昵称
     */
    private void updateName()
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token",mStrUserToken);
        params.put("name", mStrName);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.MODIFYUSERNAME, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                ToastUtils.showToast(SettingMyNameActivity.this, "昵称更换成功");
                sendChangeHeadBR(mStrName);
                SVProgressHUD.dismiss(SettingMyNameActivity.this);
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(SettingMyNameActivity.this, "昵称更换失败");
                SVProgressHUD.dismiss(SettingMyNameActivity.this);
            }
        });
    }
    /**
     * 更改数据库，并且发送更改昵称的广播
     */
    private void sendChangeHeadBR(String name)
    {
        try
        {
            //根据id查询用户
            UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(SettingMyNameActivity.this);
            UserInfoModel model = mUserinfoDaoImpl.queryReturnOne(mStrUserId);
            model.setmStrUserName(name);
            mUserinfoDaoImpl.commit();
            Intent mInent = new Intent("HEAD_CHANGE");
            this.sendBroadcast(mInent);
        }
        catch (Exception e)
        {

        }
    }
}
